import ConfigParser
import datetime
import time
import re
import os

wearehouse = './wearehouse'
if not os.path.exists(wearehouse):
    os.mkdir(wearehouse)


def handle_path():
    config = ConfigParser.ConfigParser()
    config.read('./config.ini')
    month = re.compile('{month}')
    day = re.compile('{day}')
    now = datetime.datetime.now()
    paths = []
    for path_key in filter(lambda _: _.startswith('path-'), config.sections()):
        agos = - config.getint(path_key, 'time')
        target = now + datetime.timedelta(days=agos)
        path = config.get(path_key, 'path')
        path = re.sub(month, target.strftime('%Y%m'), path)
        path = re.sub(day, target.strftime('%Y%m%d'), path)
        paths.append(path)
    return paths


def command_path(paths):
    results = []
    now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
    line = '{},{},{}\n'
    for path in paths:
        start_time = time.time()
        command = 'hadoop fs -rmr {}'.format(path)
        #os.system(command)
        print command
        use_time = time.time() - start_time
        results.append(line.format(now, command, use_time))
    return results


def main():
    paths = handle_path()
    results = command_path(paths)
    file_name = datetime.datetime.now().strftime('%Y_%m_%d_%H')
    with open('{}/{}'.format(wearehouse, file_name), mode='a') as file, \
            open('{}/all'.format(wearehouse), mode='a') as all_file:
        file.writelines(results)
        all_file.writelines(results)


if __name__ == '__main__':
    main()
